package com.xcx.robot.mirai.factory.facade;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.mirai.entity.JX3API;
import com.xcx.robot.mirai.entity.flower.Flower;
import com.xcx.robot.mirai.entity.flower.FlowerData;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.KeyUtils;
import com.xcx.robot.mirai.util.SenderHelper;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.PlainText;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 邢晨旭
 * @Date: 2021/6/22 09:33
 * @Description:
 */
@Service
public class FlowerFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.FLOWER;


    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        String flower = KeyUtils.getKeyByWord(content, 2);
        if (StringUtils.isEmpty(flower)) {
            SenderHelper.sendMessage(receiver, new PlainText("指令异常"));
            return;
        }
        String map = KeyUtils.getKeyByWord(content, 3);
        String result = getFlower("剑胆琴心", flower, map);
        if(!StringUtils.isEmpty(result)){
            SenderHelper.sendMessage(receiver,new PlainText(result));
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }

    //广陵邑 枫叶泊·天苑 枫叶泊·乐苑 九寨沟·镜海
    // 绣球花 郁金香 牵牛花 玫瑰 百合 荧光菌 羽扇豆花 葫芦 麦子 青菜 芜菁
    private String getFlower(String server, String flower, String map) {
        StringBuilder result = new StringBuilder();
        if (flower.contains("绣球")) {
            flower = "绣球花";
        } else if (flower.contains("郁金香")) {
            flower = "郁金香";
        } else if (flower.contains("牵牛")) {
            flower = "牵牛花";
        } else if (flower.contains("玫瑰")) {
            flower = "玫瑰";
        } else if (flower.contains("百合")) {
            flower = "百合";
        } else if (flower.contains("荧光")) {
            flower = "荧光菌";
        } else if (flower.contains("羽扇") || flower.contains("豆花")) {
            flower = "羽扇豆花";
        } else if (flower.contains("葫芦")) {
            flower = "葫芦";
        } else if (flower.contains("麦子")) {
            flower = "麦子";
        } else if (flower.contains("青菜")) {
            flower = "青菜";
        } else if (flower.contains("芜菁")) {
            flower = "芜菁";
        }
        if (StringUtils.isEmpty(map)) {
            //广陵邑 枫叶泊·天苑 枫叶泊·乐苑 九寨沟·镜海
            String s1 = HttpUtil.get(String.format(JX3API.FLOWER, server, flower, "广陵邑"));
            String s2 = HttpUtil.get(String.format(JX3API.FLOWER, server, flower, "枫叶泊·天苑"));
            String s3 = HttpUtil.get(String.format(JX3API.FLOWER, server, flower, "枫叶泊·乐苑"));
            String s4 = HttpUtil.get(String.format(JX3API.FLOWER, server, flower, "九寨沟·镜海"));
            Flower flower1 = JSONObject.parseObject(s1, Flower.class);
            Flower flower2 = JSONObject.parseObject(s2, Flower.class);
            Flower flower3 = JSONObject.parseObject(s3, Flower.class);
            Flower flower4 = JSONObject.parseObject(s4, Flower.class);
            List<FlowerData> data1 = flower1.getData();
            List<FlowerData> data2 = flower2.getData();
            List<FlowerData> data3 = flower3.getData();
            List<FlowerData> data4 = flower4.getData();
            if (!CollectionUtil.isEmpty(data1)) {
                result.append("广陵邑\n");
                data1.forEach(item -> {
                    result.append(item.getName()).append("(")
                            .append(item.getColor())
                            .append(")\n")
                            .append(String.join("线、", item.getLine())).append("线")
                            .append(item.getPrice()).append("倍回收\n");
                    ;
                });
            }
            if (!CollectionUtil.isEmpty(data2)) {
                result.append("枫叶泊·天苑\n");
                data1.forEach(item -> {
                    result.append(item.getName()).append("(")
                            .append(item.getColor())
                            .append(")\n")
                            .append(String.join("线、", item.getLine())).append("线")
                            .append(item.getPrice()).append("倍回收\n");
                    ;
                });
            }
            if (!CollectionUtil.isEmpty(data3)) {
                result.append("枫叶泊·乐苑\n");
                data1.forEach(item -> {
                    result.append(item.getName()).append("(")
                            .append(item.getColor())
                            .append(")\n")
                            .append(String.join("线、", item.getLine())).append("线")
                            .append(item.getPrice()).append("倍回收\n");
                    ;
                });
            }
            if (!CollectionUtil.isEmpty(data4)) {
                result.append("九寨沟·镜海\n");
                data1.forEach(item -> {
                    result.append(item.getName()).append("(")
                            .append(item.getColor())
                            .append(")\n")
                            .append(String.join("线、", item.getLine())).append("线")
                            .append(item.getPrice()).append("倍回收\n");
                    ;
                });
            }
            return result.toString();
        } else {
            //广陵邑 枫叶泊·天苑 枫叶泊·乐苑 九寨沟·镜海
            if (map.contains("广陵")) {
                map = "广陵邑";
            } else if (flower.contains("天苑")) {
                map = "枫叶泊·天苑";
            } else if (flower.contains("乐苑")) {
                map = "枫叶泊·乐苑";
            }
            else if (flower.contains("九寨沟·镜海")) {
                map = "九寨沟·镜海";
            }else {
                return null;
            }
            String s = HttpUtil.get(String.format(JX3API.FLOWER, server, flower, map));
            Flower f = JSONObject.parseObject(s, Flower.class);
            if (!CollectionUtil.isEmpty(f.getData())) {
                result.append(map).append("\n");
                f.getData().forEach(item -> {
                    result.append(item.getName()).append("(")
                            .append(item.getColor())
                            .append(")\n")
                            .append(String.join("线、", item.getLine())).append("线")
                            .append(item.getPrice()).append("倍回收");
                    ;
                });
                return result.toString();
            }
        }
        return null;
    }
}
